package com.demo.viewflipperdemo;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private ViewFlipper flipper;
    private int imgIds[] = {R.drawable.baiyang, R.drawable.chunv, R.drawable.jinniu, R.drawable.juxie, R.drawable.mojie, R.drawable.sheshou};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        flipper = findViewById(R.id.flipper);
        for (int i = 0; i < imgIds.length; i++) {
            ImageView img = new ImageView(this);
            img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            img.setScaleType(ImageView.ScaleType.FIT_XY);
            img.setImageResource(imgIds[i]);
            flipper.addView(img);
        }
    }

    public void prev(View v) {
        flipper.setInAnimation(this, R.anim.slide_in_right);
        flipper.setOutAnimation(this, R.anim.slide_out_left);
        flipper.showPrevious();
        flipper.stopFlipping();
    }

    public void next(View v) {
        flipper.setInAnimation(this, android.R.anim.slide_in_left);
        flipper.setOutAnimation(this, android.R.anim.slide_out_right);
        flipper.showNext();
        flipper.stopFlipping();
    }

    public void auto(View v) {
        flipper.setInAnimation(this, android.R.anim.slide_in_left);
        flipper.setOutAnimation(this, android.R.anim.slide_out_right);
        flipper.startFlipping();
    }
}
